<?php


include('db.php');

 

$user_list = Db::table('users')->field('id,name,user_type,email')->where(['is_virtual_user'=>1,'user_type'=>'customer'])->order('RAND()')->limit(99)->select();


 



foreach ($user_list as $k=>$v){
    
    
    
    
     $list = Db::table('addresses')->field('address,country_id,city_id,postal_code,phone')->where(['user_id'=>$v['id']])->find();
     
     

     
     $add['name'] = $v['name'];
     $add['email'] = $v['email'];
     $add['address'] = $list['address'];
     
     $add['country'] = '';
     $add['city'] = '';
     
     
     $add['postal_code'] = $list['postal_code'];
     $add['phone'] = $list['phone'];
     
     $user_list[$k]['addresses'] = json_encode($add);
    
}
 
 

$stime = strtotime($_REQUEST['date']);

$etime = strtotime($_REQUEST['date1s']);


if($etime < $stime) {
    
    $data['error'] = 1;
    
    $data['msg'] = '结束时间不能小于开始时间';
    
    echo   json_encode($data);  exit;
}


 

if($stime<time()) {
    
    $data['error'] = 1;
    
    $data['msg'] = '开始时间不能小于当前时间';
    
    echo  json_encode($data);  exit;
}



if($_REQUEST['time'] == '') {
    
    $data['error'] = 1;
    
    $data['msg'] = '请设置随机时间';
    
    echo  json_encode($data);  exit;
}



if($_REQUEST['time2'] == '') {
    
    $data['error'] = 1;
    
    $data['msg'] = '请设置随机时间';
    
    echo  json_encode($data);  exit;
}



 

$ins_data['dnum'] = $_REQUEST['dnum']; 
$ins_data['date'] =  strtotime($_REQUEST['date']) ; 
$ins_data['date1s'] = strtotime($_REQUEST['date1s']); 
$ins_data['time'] = $_REQUEST['time']; 
$ins_data['time2'] = $_REQUEST['time2']; 
$ins_data['seller_id'] = $_REQUEST['seller_id'];


$name = Db::table('shops')->field('id,name,user_id')->where(['user_id'=>$ins_data['seller_id']])->find();

$ins_data['seller_name'] = $name['name'];


$ins_data['addtime'] = time();

$ins_data['snum'] = 0;


$res = Db::table('newpos')->add($ins_data);

 
$data_id = $res;

if($data_id) {
    
    
$gn = 0;  
for ($i=1; $i<=$_REQUEST['dnum']; $i++){
         
          
          $s = count($user_list);
          
          $k = rand(0,$s);
          
          if(count($_REQUEST['goods_id']) >$gn ) {
              
              $pod['goods_id'] = $_REQUEST['goods_id'][$gn];
              
              $gn++;
          }  else {
              $gn = 0;
              $pod['goods_id'] = $_REQUEST['goods_id'][$gn];
          }          
          
          
       
          
          $pod['pos_id'] = $data_id;
          $pod['user_name'] = $user_list[$k]['name'];
          $pod['user_id'] = $user_list[$k]['id'];
          
          $sd = rand($stime,$etime) + (rand($_REQUEST['time'],$_REQUEST['time2'])) ;
          $pod['stime'] =$sd;
          
          $pod['sdate'] =date('Y-m-d H:i:s',$sd);
          $pod['zt'] = 0;
          $pod['user_addr'] = $user_list[$k]['addresses'];
          $pod['seller_id'] = $_REQUEST['seller_id'];
          $pod['seller_name'] = $name['name'];
          
          
          $ress = Db::table('newpos_data')->add($pod);
         
          
 
        
    
   } 
}


$data['error'] = 0;
    
$data['msg'] = '任务添加成功';
    
echo   json_encode($data);  exit;




?>